{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、加载数据包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Miniconda\\envs\\geo\\lib\\site-packages\\numpy\\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:\n",
      "d:\\Miniconda\\envs\\geo\\lib\\site-packages\\numpy\\.libs\\libopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll\n",
      "d:\\Miniconda\\envs\\geo\\lib\\site-packages\\numpy\\.libs\\libopenblas64__v0.3.21-gcc_10_3_0.dll\n",
      "  warnings.warn(\"loaded more than 1 DLL from .libs:\"\n"
     ]
    }
   ],
   "source": [
    "# 参考资料\n",
    "# https://blog.csdn.net/wizardforcel/article/details/138815429\n",
    "\n",
    "# 基础包\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import math\n",
    "import datetime as dt\n",
    "import os\n",
    "# 可视化包\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "# 可视化工具安装 pip install plotly\n",
    "import plotly.express as px\n",
    "import plotly.graph_objs as go\n",
    "from plotly.subplots import make_subplots\n",
    "\n",
    "# 模型模块\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "from d2l import torch as d2l\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.model_selection import train_test_split,GridSearchCV#导入数据划分器\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.metrics import mean_absolute_error\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义数据目录\n",
    "data_dir =  'E:/code/datasets/StoreSalesTimeSeriesForecasting/'\n",
    "# 加载训练数据\n",
    "data_train = pd.read_csv(data_dir + 'train.csv')\n",
    "# 加载测试数据\n",
    "data_test = pd.read_csv(data_dir + 'test.csv')  \n",
    "# 加载油价数据\n",
    "data_oil = pd.read_csv(data_dir + 'oil.csv')\n",
    "# 加载假期数据\n",
    "data_holi = pd.read_csv(data_dir + 'holidays_events.csv')\n",
    "# 加载门店数据\n",
    "data_store = pd.read_csv(data_dir + 'stores.csv')\n",
    "# 加载交易数据\n",
    "data_trans = pd.read_csv(data_dir + 'transactions.csv')\n",
    "# 加载提交数据\n",
    "samp_subm = pd.read_csv(data_dir + 'sample_submission.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、数据处理部分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_trans[\"date\"] = pd.to_datetime(data_trans.date)\n",
    "data_train[\"date\"] = pd.to_datetime(data_train.date)\n",
    "data_test[\"date\"] = pd.to_datetime(data_test.date)\n",
    "data_oil[\"date\"] = pd.to_datetime(data_oil.date)\n",
    "data_holi[\"date\"] = pd.to_datetime(data_holi.date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练数据大小：(3000888, 6)\n",
      "测试数据大小：(28512, 5)\n",
      "油价数据大小：(1218, 2)\n",
      "假期数据大小：(350, 6)\n",
      "门店数据大小：(54, 5)\n",
      "交易数据大小：(83488, 3)\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3000888 entries, 0 to 3000887\n",
      "Data columns (total 6 columns):\n",
      " #   Column       Dtype         \n",
      "---  ------       -----         \n",
      " 0   id           int64         \n",
      " 1   date         datetime64[ns]\n",
      " 2   store_nbr    int64         \n",
      " 3   family       object        \n",
      " 4   sales        float64       \n",
      " 5   onpromotion  int64         \n",
      "dtypes: datetime64[ns](1), float64(1), int64(3), object(1)\n",
      "memory usage: 137.4+ MB\n",
      "None\n",
      "Index(['id', 'date', 'store_nbr', 'family', 'sales', 'onpromotion'], dtype='object')\n",
      "   id       date  store_nbr      family  sales  onpromotion\n",
      "0   0 2013-01-01          1  AUTOMOTIVE    0.0            0\n",
      "1   1 2013-01-01          1   BABY CARE    0.0            0\n",
      "2   2 2013-01-01          1      BEAUTY    0.0            0\n",
      "3   3 2013-01-01          1   BEVERAGES    0.0            0\n",
      "4   4 2013-01-01          1       BOOKS    0.0            0\n"
     ]
    }
   ],
   "source": [
    "print(\"训练数据大小：{}\".format(data_train.shape))\n",
    "print(\"测试数据大小：{}\".format(data_test.shape))\n",
    "print(\"油价数据大小：{}\".format(data_oil.shape))\n",
    "print(\"假期数据大小：{}\".format(data_holi.shape))\n",
    "print(\"门店数据大小：{}\".format(data_store.shape))\n",
    "print(\"交易数据大小：{}\".format(data_trans.shape))\n",
    "print(data_train.info())\n",
    "print(data_train.columns)\n",
    "print(data_train.head())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 测试插值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data_oil #1218行\n",
    "# data_oil.dcoilwtico.interpolate()\n",
    "\n",
    "# import pandas as pd    \n",
    "# oil = pd.DataFrame()\n",
    "# origin = pd.Series([1, 2, np.nan,0,12,0, 4, 5])\n",
    "# oil[\"dcoilwtico\"] = origin\n",
    "# oil[\"dcoilwtico\"] = np.where(oil[\"dcoilwtico\"] == 0, np.nan, oil[\"dcoilwtico\"])\n",
    "# #  必须先将0值转换为NaN值才能进行插值处理\n",
    "# oil[\"dcoilwtico_interpolated\"] = oil.dcoilwtico.interpolate()\n",
    "# # # #结合 oil[\"dcoilwtico\"] 和 oil[\"dcoilwtico_interpolated\"] \n",
    "# oil['all_dcoilwtico'] = oil['dcoilwtico'].combine_first(oil['dcoilwtico_interpolated'])#这里就是用interpolated的值来填充原来的的空值\n",
    "# oil\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 插值处理厄瓜多尔的油价信息并显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan 93.14\n",
      "        date  dcoilwtico\n",
      "0 2013-01-01       93.14\n",
      "1 2013-01-02       93.14\n",
      "2 2013-01-03       92.97\n",
      "3 2013-01-04       93.12\n",
      "4 2013-01-07       93.20\n",
      "           date  dcoilwtico\n",
      "1213 2017-08-25       47.65\n",
      "1214 2017-08-28       46.40\n",
      "1215 2017-08-29       46.46\n",
      "1216 2017-08-30       45.96\n",
      "1217 2017-08-31       47.26\n",
      "date          0\n",
      "dcoilwtico    0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "oil = data_oil.copy()\n",
    "\"\"\"\n",
    "检查 oil DataFrame中 \"dcoilwtico\" 列的每个元素是否等于0。\n",
    "将dcoilwtico列中所有为0的值的信息转换为Nan以便进行插值处理。\n",
    "\"\"\"\n",
    "# 插值\n",
    "oil[\"dcoilwtico\"] = np.where(oil[\"dcoilwtico\"] == 0, np.nan, oil[\"dcoilwtico\"])\n",
    "oil[\"dcoilwtico_interpolated\"] = oil.dcoilwtico.interpolate()\n",
    "\n",
    "# #结合 oil[\"dcoilwtico\"] 和 oil[\"dcoilwtico_interpolated\"] \n",
    "oil['all_dcoilwtico'] = oil['dcoilwtico'].combine_first(oil['dcoilwtico_interpolated']) # 这里就是用interpolated的值来填充原来的的空值\n",
    "# 去掉两列不需要的列\n",
    "oil = oil.drop(['dcoilwtico', 'dcoilwtico_interpolated'], axis=1)\n",
    "oil = oil.rename({'all_dcoilwtico': 'dcoilwtico'}, axis=1)\n",
    "# 这里能看到假日是没有交易数据的。但是在图形中会展现x轴上跳过两天的节假日长度\n",
    "print(oil.iloc[0]['dcoilwtico'],oil.iloc[1]['dcoilwtico'])\n",
    "\n",
    "# 先将开头的油价信息为Nan的信息补全\n",
    "oil.iloc[0, oil.columns.get_loc('dcoilwtico')] = oil.iloc[1]['dcoilwtico']\n",
    "print(oil.head())\n",
    "print(oil.tail())\n",
    "print(oil.isnull().sum())\n",
    "\n",
    "# # 画图认识一下plotly：https://plotly.com/graphing-libraries/\n",
    "# fig = px.line(oil, x='date', y='dcoilwtico', title = \"厄瓜多尔油价信息\")\n",
    "# fig.update_traces(line_color='sienna') \n",
    "# fig.show(\"notebook\")\n",
    "# print(data_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 每日平均销售量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df_test_New             id       date  store_nbr                      family  onpromotion  \\\n",
      "0      3000888 2017-08-16          1                  AUTOMOTIVE            0   \n",
      "1      3000889 2017-08-16          1                   BABY CARE            0   \n",
      "2      3000890 2017-08-16          1                      BEAUTY            2   \n",
      "3      3000891 2017-08-16          1                   BEVERAGES           20   \n",
      "4      3000892 2017-08-16          1                       BOOKS            0   \n",
      "...        ...        ...        ...                         ...          ...   \n",
      "28507  3029395 2017-08-31          9                     POULTRY            1   \n",
      "28508  3029396 2017-08-31          9              PREPARED FOODS            0   \n",
      "28509  3029397 2017-08-31          9                     PRODUCE            1   \n",
      "28510  3029398 2017-08-31          9  SCHOOL AND OFFICE SUPPLIES            9   \n",
      "28511  3029399 2017-08-31          9                     SEAFOOD            0   \n",
      "\n",
      "      Daily_holiday_type Daily_holiday_locale Daily_holiday_locale_name  \\\n",
      "0                    NaN                  NaN                       NaN   \n",
      "1                    NaN                  NaN                       NaN   \n",
      "2                    NaN                  NaN                       NaN   \n",
      "3                    NaN                  NaN                       NaN   \n",
      "4                    NaN                  NaN                       NaN   \n",
      "...                  ...                  ...                       ...   \n",
      "28507                NaN                  NaN                       NaN   \n",
      "28508                NaN                  NaN                       NaN   \n",
      "28509                NaN                  NaN                       NaN   \n",
      "28510                NaN                  NaN                       NaN   \n",
      "28511                NaN                  NaN                       NaN   \n",
      "\n",
      "      Daily_holiday_description Daily_holiday_transferred  dcoilwtico  \\\n",
      "0                           NaN                       NaN       46.80   \n",
      "1                           NaN                       NaN       46.80   \n",
      "2                           NaN                       NaN       46.80   \n",
      "3                           NaN                       NaN       46.80   \n",
      "4                           NaN                       NaN       46.80   \n",
      "...                         ...                       ...         ...   \n",
      "28507                       NaN                       NaN       47.26   \n",
      "28508                       NaN                       NaN       47.26   \n",
      "28509                       NaN                       NaN       47.26   \n",
      "28510                       NaN                       NaN       47.26   \n",
      "28511                       NaN                       NaN       47.26   \n",
      "\n",
      "      stores_city store_state store_type  store_cluster  transactions  \n",
      "0           Quito   Pichincha          D             13           NaN  \n",
      "1           Quito   Pichincha          D             13           NaN  \n",
      "2           Quito   Pichincha          D             13           NaN  \n",
      "3           Quito   Pichincha          D             13           NaN  \n",
      "4           Quito   Pichincha          D             13           NaN  \n",
      "...           ...         ...        ...            ...           ...  \n",
      "28507       Quito   Pichincha          B              6           NaN  \n",
      "28508       Quito   Pichincha          B              6           NaN  \n",
      "28509       Quito   Pichincha          B              6           NaN  \n",
      "28510       Quito   Pichincha          B              6           NaN  \n",
      "28511       Quito   Pichincha          B              6           NaN  \n",
      "\n",
      "[28512 rows x 16 columns]\n"
     ]
    }
   ],
   "source": [
    "def Add_Feature():\n",
    "    data_holi.rename(columns={'date': 'date',\n",
    "                                       'type': 'Daily_holiday_type',\n",
    "                                       'locale': 'Daily_holiday_locale',\n",
    "                                       'locale_name': 'Daily_holiday_locale_name',\n",
    "                                       'description': \"Daily_holiday_description\",\n",
    "                                       'transferred': \"Daily_holiday_transferred\"},\n",
    "                              inplace=True)\n",
    "    data_store.rename(columns={'store_nbr': 'store_nbr',\n",
    "                              'city': 'stores_city',\n",
    "                              'state': 'store_state',\n",
    "                              'type': 'store_type',\n",
    "                              'cluster': 'store_cluster'},\n",
    "                     inplace=True)\n",
    "    data_trans.rename(columns={'transactions': 'Daily_transactions'})\n",
    "    DfTrainNew = pd.merge(data_train, data_holi, how='left', left_on='date', right_on='date')\n",
    "    DfTestNew = pd.merge(data_test, data_holi, how='left', left_on='date', right_on='date')\n",
    "    DfTrainNew = pd.merge(DfTrainNew, oil, how='left', left_on='date', right_on='date')\n",
    "    DfTestNew = pd.merge(DfTestNew, oil, how='left', left_on='date', right_on='date')\n",
    "    DfTrainNew = pd.merge(DfTrainNew, data_store, how='left', left_on='store_nbr', right_on='store_nbr')\n",
    "    DfTestNew = pd.merge(DfTestNew, data_store, how='left', left_on='store_nbr', right_on='store_nbr')\n",
    "    DfTrainNew = pd.merge(DfTrainNew, data_trans, how='left', on=['date', 'store_nbr'])\n",
    "    DfTestNew = pd.merge(DfTestNew, data_trans, how='left', on=['date', 'store_nbr'])\n",
    "    return DfTrainNew, DfTestNew\n",
    "\n",
    "# print(oil.isnull().sum())\n",
    "res = Add_Feature()\n",
    "df_train_New = res[0]\n",
    "df_test_New = res[1]\n",
    "print(\"df_test_New\",df_test_New)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # print(df_train_New)\n",
    "# print(df_test_New)\n",
    "# new_train_date_data = df_train_New.sort_values('date').groupby('date')[\"id\"].min().reset_index()\n",
    "# new_test_date_data = df_test_New.sort_values('date').groupby('date')[\"id\"].min().reset_index()\n",
    "# # print(new_train_date_data)\n",
    "# print(new_test_date_data)   \n",
    "# oil_new = pd.merge(oil, new_train_date_data, how='left', left_on='date', right_on='date')\n",
    "# print(oil_new.isnull().sum())\n",
    "# oil_new = pd.merge(oil, new_test_date_data, how='left', left_on='date', right_on='date')\n",
    "# print(oil_new.isnull().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 给出一段id列表，将其中自然数连续的部分且分开，并取得其连续列表中前一个id值组成列表\n",
    "def get_nan_id_list_and_last_id_list(first_nan_idx):\n",
    "    first_id = first_nan_idx[0]\n",
    "    nan_first_id_list = [first_nan_idx[0]]\n",
    "    nan_list = []\n",
    "    tmp_list = []\n",
    "    for i in first_nan_idx:\n",
    "        if i == first_id:\n",
    "            first_id+=1\n",
    "            tmp_list.append(i)\n",
    "        else:\n",
    "            nan_first_id_list.append(i)\n",
    "            first_id = i+1\n",
    "            nan_list.append(tmp_list)\n",
    "            tmp_list = [i]\n",
    "    nan_list.append(tmp_list)\n",
    "    nonan_last_id_list = [i-1 for i in nan_first_id_list]\n",
    "    return nan_list,nonan_last_id_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id                                0\n",
      "date                              0\n",
      "store_nbr                         0\n",
      "family                            0\n",
      "sales                             0\n",
      "onpromotion                       0\n",
      "Daily_holiday_type                0\n",
      "Daily_holiday_locale              0\n",
      "Daily_holiday_locale_name         0\n",
      "Daily_holiday_description         0\n",
      "Daily_holiday_transferred         0\n",
      "dcoilwtico                        0\n",
      "stores_city                       0\n",
      "store_state                       0\n",
      "store_type                        0\n",
      "store_cluster                     0\n",
      "transactions                 249117\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "first_nan_idx = df_train_New[df_train_New['dcoilwtico'].isnull()].index\n",
    "nan_list,nonan_last_id_list =get_nan_id_list_and_last_id_list(first_nan_idx)\n",
    "for i in range(len(nan_list)):\n",
    "    df_train_New.loc[nan_list[i],'dcoilwtico'] = df_train_New.loc[nonan_last_id_list[i], 'dcoilwtico']\n",
    "\n",
    "df_train_New['Daily_holiday_type'] = df_train_New['Daily_holiday_type'].fillna('Workday')\n",
    "df_train_New['Daily_holiday_locale'] = df_train_New['Daily_holiday_locale'].fillna('Workday')\n",
    "df_train_New['Daily_holiday_locale_name'] = df_train_New['Daily_holiday_locale_name'].fillna('Workday')\n",
    "df_train_New['Daily_holiday_description'] = df_train_New['Daily_holiday_description'].fillna('Workday')\n",
    "df_train_New['Daily_holiday_transferred'] = df_train_New['Daily_holiday_transferred'].fillna(False)\n",
    "print(df_train_New.isnull().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 28512 entries, 0 to 28511\n",
      "Data columns (total 16 columns):\n",
      " #   Column                     Non-Null Count  Dtype         \n",
      "---  ------                     --------------  -----         \n",
      " 0   id                         28512 non-null  int64         \n",
      " 1   date                       28512 non-null  datetime64[ns]\n",
      " 2   store_nbr                  28512 non-null  int64         \n",
      " 3   family                     28512 non-null  object        \n",
      " 4   onpromotion                28512 non-null  int64         \n",
      " 5   Daily_holiday_type         28512 non-null  object        \n",
      " 6   Daily_holiday_locale       28512 non-null  object        \n",
      " 7   Daily_holiday_locale_name  28512 non-null  object        \n",
      " 8   Daily_holiday_description  28512 non-null  object        \n",
      " 9   Daily_holiday_transferred  28512 non-null  bool          \n",
      " 10  dcoilwtico                 28512 non-null  float64       \n",
      " 11  stores_city                28512 non-null  object        \n",
      " 12  store_state                28512 non-null  object        \n",
      " 13  store_type                 28512 non-null  object        \n",
      " 14  store_cluster              28512 non-null  int64         \n",
      " 15  transactions               0 non-null      float64       \n",
      "dtypes: bool(1), datetime64[ns](1), float64(2), int64(4), object(8)\n",
      "memory usage: 3.3+ MB\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# print(df_test_New.info())\n",
    "# 查看缺失值\n",
    "first_nan_idx = df_test_New[df_test_New['dcoilwtico'].isnull()].index\n",
    "nan_list,nonan_last_id_list = get_nan_id_list_and_last_id_list(first_nan_idx)\n",
    "for i in range(len(nan_list)):\n",
    "    df_test_New.loc[nan_list[i],'dcoilwtico'] = df_test_New.loc[nonan_last_id_list[i], 'dcoilwtico']\n",
    "df_test_New['Daily_holiday_type'] = df_test_New['Daily_holiday_type'].fillna('Workday')\n",
    "df_test_New['Daily_holiday_locale'] = df_test_New['Daily_holiday_locale'].fillna('Workday')\n",
    "df_test_New['Daily_holiday_locale_name'] = df_test_New['Daily_holiday_locale_name'].fillna('Workday')\n",
    "df_test_New['Daily_holiday_description'] = df_test_New['Daily_holiday_description'].fillna('Workday')\n",
    "df_test_New['Daily_holiday_transferred'] = df_test_New['Daily_holiday_transferred'].fillna(False)\n",
    "# print(df_test_New.isnull().sum())\n",
    "print(df_test_New.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 处理训练数据\n",
    "# df_train_New.drop_duplicates(subset='id', keep='first', inplace=True)\n",
    "# df_train_New.dropna(axis=0, inplace=True)\n",
    "# # 处理测试数据\n",
    "# df_test_New.drop_duplicates(subset='id', keep='first', inplace=True)\n",
    "# df_test_New.dropna(axis=0, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3054348 entries, 0 to 3054347\n",
      "Data columns (total 17 columns):\n",
      " #   Column                     Dtype         \n",
      "---  ------                     -----         \n",
      " 0   id                         int64         \n",
      " 1   date                       datetime64[ns]\n",
      " 2   store_nbr                  int64         \n",
      " 3   family                     object        \n",
      " 4   sales                      float64       \n",
      " 5   onpromotion                int64         \n",
      " 6   Daily_holiday_type         object        \n",
      " 7   Daily_holiday_locale       object        \n",
      " 8   Daily_holiday_locale_name  object        \n",
      " 9   Daily_holiday_description  object        \n",
      " 10  Daily_holiday_transferred  bool          \n",
      " 11  dcoilwtico                 float64       \n",
      " 12  stores_city                object        \n",
      " 13  store_state                object        \n",
      " 14  store_type                 object        \n",
      " 15  store_cluster              int64         \n",
      " 16  transactions               float64       \n",
      "dtypes: bool(1), datetime64[ns](1), float64(3), int64(4), object(8)\n",
      "memory usage: 375.8+ MB\n"
     ]
    }
   ],
   "source": [
    "df_train_New.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 28512 entries, 0 to 28511\n",
      "Data columns (total 16 columns):\n",
      " #   Column                     Non-Null Count  Dtype         \n",
      "---  ------                     --------------  -----         \n",
      " 0   id                         28512 non-null  int64         \n",
      " 1   date                       28512 non-null  datetime64[ns]\n",
      " 2   store_nbr                  28512 non-null  int64         \n",
      " 3   family                     28512 non-null  object        \n",
      " 4   onpromotion                28512 non-null  int64         \n",
      " 5   Daily_holiday_type         28512 non-null  object        \n",
      " 6   Daily_holiday_locale       28512 non-null  object        \n",
      " 7   Daily_holiday_locale_name  28512 non-null  object        \n",
      " 8   Daily_holiday_description  28512 non-null  object        \n",
      " 9   Daily_holiday_transferred  28512 non-null  bool          \n",
      " 10  dcoilwtico                 28512 non-null  float64       \n",
      " 11  stores_city                28512 non-null  object        \n",
      " 12  store_state                28512 non-null  object        \n",
      " 13  store_type                 28512 non-null  object        \n",
      " 14  store_cluster              28512 non-null  int64         \n",
      " 15  transactions               0 non-null      float64       \n",
      "dtypes: bool(1), datetime64[ns](1), float64(2), int64(4), object(8)\n",
      "memory usage: 3.3+ MB\n"
     ]
    }
   ],
   "source": [
    "df_test_New.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.preprocessing import OneHotEncoder  \n",
    "# import numpy as np  \n",
    "  \n",
    "# # 假设你有一个 NumPy 数组，其中包含了类别数据（需要是整数或字符串）  \n",
    "# # 通常，你需要先将字符串类别转换为整数编码（例如，使用 LabelEncoder）  \n",
    "# categories = np.array(['A', 'B', 'A', 'C', 'B', 'A']).reshape(-1, 1)  \n",
    "  \n",
    "# # 如果你有字符串数据，你需要先使用 LabelEncoder 转换为整数  \n",
    "# # from sklearn.preprocessing import LabelEncoder  \n",
    "# # label_encoder = LabelEncoder()  \n",
    "# # integer_encoded = label_encoder.fit_transform(categories.ravel())  \n",
    "# # categories_encoded = integer_encoded.reshape(len(categories), 1)  \n",
    "  \n",
    "# # 但在这个例子中，我们假设 categories 已经是整数编码的  \n",
    "# encoder = OneHotEncoder(sparse=False)  # sparse=False 返回普通的 NumPy 数组而不是稀疏矩阵  \n",
    "# onehot_encoded = encoder.fit_transform(categories)  \n",
    "# print(onehot_encoded)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.preprocessing import LabelEncoder\n",
    "# le = LabelEncoder()\n",
    "# le.fit([\"paris\", \"paris\", \"tokyo\", \"amsterdam\",\"paris\", \"tokyo\", \"amsterdam\"])\n",
    "# print(list(le.classes_))\n",
    "# print(le.transform([ \"paris\", \"tokyo\", \"amsterdam\",\"paris\", \"tokyo\", \"amsterdam\"]))\n",
    "# list(le.inverse_transform([2, 2, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>date</th>\n",
       "      <th>store_nbr</th>\n",
       "      <th>family</th>\n",
       "      <th>onpromotion</th>\n",
       "      <th>Daily_holiday_type</th>\n",
       "      <th>Daily_holiday_locale</th>\n",
       "      <th>Daily_holiday_locale_name</th>\n",
       "      <th>Daily_holiday_description</th>\n",
       "      <th>Daily_holiday_transferred</th>\n",
       "      <th>dcoilwtico</th>\n",
       "      <th>stores_city</th>\n",
       "      <th>store_state</th>\n",
       "      <th>store_type</th>\n",
       "      <th>store_cluster</th>\n",
       "      <th>transactions</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3000888</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>1</td>\n",
       "      <td>AUTOMOTIVE</td>\n",
       "      <td>0</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>46.80</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>D</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3000889</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>1</td>\n",
       "      <td>BABY CARE</td>\n",
       "      <td>0</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>46.80</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>D</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3000890</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>1</td>\n",
       "      <td>BEAUTY</td>\n",
       "      <td>2</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>46.80</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>D</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3000891</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>1</td>\n",
       "      <td>BEVERAGES</td>\n",
       "      <td>20</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>46.80</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>D</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3000892</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>1</td>\n",
       "      <td>BOOKS</td>\n",
       "      <td>0</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>46.80</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>D</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28507</th>\n",
       "      <td>3029395</td>\n",
       "      <td>2017-08-31</td>\n",
       "      <td>9</td>\n",
       "      <td>POULTRY</td>\n",
       "      <td>1</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>47.26</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>B</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28508</th>\n",
       "      <td>3029396</td>\n",
       "      <td>2017-08-31</td>\n",
       "      <td>9</td>\n",
       "      <td>PREPARED FOODS</td>\n",
       "      <td>0</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>47.26</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>B</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28509</th>\n",
       "      <td>3029397</td>\n",
       "      <td>2017-08-31</td>\n",
       "      <td>9</td>\n",
       "      <td>PRODUCE</td>\n",
       "      <td>1</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>47.26</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>B</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28510</th>\n",
       "      <td>3029398</td>\n",
       "      <td>2017-08-31</td>\n",
       "      <td>9</td>\n",
       "      <td>SCHOOL AND OFFICE SUPPLIES</td>\n",
       "      <td>9</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>47.26</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>B</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28511</th>\n",
       "      <td>3029399</td>\n",
       "      <td>2017-08-31</td>\n",
       "      <td>9</td>\n",
       "      <td>SEAFOOD</td>\n",
       "      <td>0</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>Workday</td>\n",
       "      <td>False</td>\n",
       "      <td>47.26</td>\n",
       "      <td>Quito</td>\n",
       "      <td>Pichincha</td>\n",
       "      <td>B</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>28512 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            id       date  store_nbr                      family  onpromotion  \\\n",
       "0      3000888 2017-08-16          1                  AUTOMOTIVE            0   \n",
       "1      3000889 2017-08-16          1                   BABY CARE            0   \n",
       "2      3000890 2017-08-16          1                      BEAUTY            2   \n",
       "3      3000891 2017-08-16          1                   BEVERAGES           20   \n",
       "4      3000892 2017-08-16          1                       BOOKS            0   \n",
       "...        ...        ...        ...                         ...          ...   \n",
       "28507  3029395 2017-08-31          9                     POULTRY            1   \n",
       "28508  3029396 2017-08-31          9              PREPARED FOODS            0   \n",
       "28509  3029397 2017-08-31          9                     PRODUCE            1   \n",
       "28510  3029398 2017-08-31          9  SCHOOL AND OFFICE SUPPLIES            9   \n",
       "28511  3029399 2017-08-31          9                     SEAFOOD            0   \n",
       "\n",
       "      Daily_holiday_type Daily_holiday_locale Daily_holiday_locale_name  \\\n",
       "0                Workday              Workday                   Workday   \n",
       "1                Workday              Workday                   Workday   \n",
       "2                Workday              Workday                   Workday   \n",
       "3                Workday              Workday                   Workday   \n",
       "4                Workday              Workday                   Workday   \n",
       "...                  ...                  ...                       ...   \n",
       "28507            Workday              Workday                   Workday   \n",
       "28508            Workday              Workday                   Workday   \n",
       "28509            Workday              Workday                   Workday   \n",
       "28510            Workday              Workday                   Workday   \n",
       "28511            Workday              Workday                   Workday   \n",
       "\n",
       "      Daily_holiday_description  Daily_holiday_transferred  dcoilwtico  \\\n",
       "0                       Workday                      False       46.80   \n",
       "1                       Workday                      False       46.80   \n",
       "2                       Workday                      False       46.80   \n",
       "3                       Workday                      False       46.80   \n",
       "4                       Workday                      False       46.80   \n",
       "...                         ...                        ...         ...   \n",
       "28507                   Workday                      False       47.26   \n",
       "28508                   Workday                      False       47.26   \n",
       "28509                   Workday                      False       47.26   \n",
       "28510                   Workday                      False       47.26   \n",
       "28511                   Workday                      False       47.26   \n",
       "\n",
       "      stores_city store_state store_type  store_cluster  transactions  \n",
       "0           Quito   Pichincha          D             13           NaN  \n",
       "1           Quito   Pichincha          D             13           NaN  \n",
       "2           Quito   Pichincha          D             13           NaN  \n",
       "3           Quito   Pichincha          D             13           NaN  \n",
       "4           Quito   Pichincha          D             13           NaN  \n",
       "...           ...         ...        ...            ...           ...  \n",
       "28507       Quito   Pichincha          B              6           NaN  \n",
       "28508       Quito   Pichincha          B              6           NaN  \n",
       "28509       Quito   Pichincha          B              6           NaN  \n",
       "28510       Quito   Pichincha          B              6           NaN  \n",
       "28511       Quito   Pichincha          B              6           NaN  \n",
       "\n",
       "[28512 rows x 16 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test_New"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3054348 entries, 0 to 3054347\n",
      "Data columns (total 17 columns):\n",
      " #   Column                     Dtype         \n",
      "---  ------                     -----         \n",
      " 0   id                         int64         \n",
      " 1   date                       datetime64[ns]\n",
      " 2   store_nbr                  int64         \n",
      " 3   family                     int32         \n",
      " 4   sales                      float64       \n",
      " 5   onpromotion                int64         \n",
      " 6   Daily_holiday_type         int32         \n",
      " 7   Daily_holiday_locale       int32         \n",
      " 8   Daily_holiday_locale_name  int32         \n",
      " 9   Daily_holiday_description  int32         \n",
      " 10  Daily_holiday_transferred  int64         \n",
      " 11  dcoilwtico                 float64       \n",
      " 12  stores_city                int32         \n",
      " 13  store_state                int32         \n",
      " 14  store_type                 int32         \n",
      " 15  store_cluster              int64         \n",
      " 16  transactions               float64       \n",
      "dtypes: datetime64[ns](1), float64(3), int32(8), int64(5)\n",
      "memory usage: 302.9 MB\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 使用Onehot类来处理编码问题\n",
    "class OneHotProcess(LabelEncoder):\n",
    "    def __init__(self,le_name):\n",
    "        self.name = le_name\n",
    "        super(OneHotProcess,self).__init__()\n",
    "\n",
    "    def run_label_handler(self,df,one_hot_name):\n",
    "        self.fit(df[one_hot_name])\n",
    "        self.one_hot_res = self.transform(df_train_New[one_hot_name])\n",
    "        self.label_list = list(self.inverse_transform([i for i in range(len(self.classes_))]))\n",
    "\n",
    "    def test_run(self,df,one_hot_name):\n",
    "        return self.transform(df[one_hot_name])\n",
    "\n",
    "family_handler = OneHotProcess(\"family\")\n",
    "family_handler.run_label_handler(df_train_New,'family')\n",
    "one_hot_res = family_handler.one_hot_res\n",
    "df_train_New[\"family\"] = one_hot_res\n",
    "\n",
    "Daily_holiday_type_handler = OneHotProcess(\"Daily_holiday_type\")\n",
    "Daily_holiday_type_handler.run_label_handler(df_train_New,'Daily_holiday_type')\n",
    "one_hot_res = Daily_holiday_type_handler.one_hot_res\n",
    "df_train_New[\"Daily_holiday_type\"] = one_hot_res\n",
    "\n",
    "\n",
    "Daily_holiday_locale_handler = OneHotProcess(\"Daily_holiday_locale\")\n",
    "Daily_holiday_locale_handler.run_label_handler(df_train_New,'Daily_holiday_locale')\n",
    "one_hot_res = Daily_holiday_locale_handler.one_hot_res\n",
    "df_train_New[\"Daily_holiday_locale\"] = one_hot_res\n",
    "\n",
    "Daily_holiday_locale_name_handler = OneHotProcess(\"Daily_holiday_locale_name\")\n",
    "Daily_holiday_locale_name_handler.run_label_handler(df_train_New,'Daily_holiday_locale_name')\n",
    "one_hot_res = Daily_holiday_locale_name_handler.one_hot_res\n",
    "df_train_New[\"Daily_holiday_locale_name\"] = one_hot_res\n",
    "\n",
    "\n",
    "Daily_holiday_description_handler = OneHotProcess(\"Daily_holiday_description\")\n",
    "Daily_holiday_description_handler.run_label_handler(df_train_New,'Daily_holiday_description')\n",
    "one_hot_res = Daily_holiday_description_handler.one_hot_res\n",
    "df_train_New[\"Daily_holiday_description\"] = one_hot_res\n",
    "\n",
    "Daily_holiday_transferred_handler = OneHotProcess(\"Daily_holiday_transferred\")\n",
    "Daily_holiday_transferred_handler.run_label_handler(df_train_New,'Daily_holiday_transferred')\n",
    "one_hot_res = Daily_holiday_transferred_handler.one_hot_res\n",
    "df_train_New[\"Daily_holiday_transferred\"] = one_hot_res\n",
    "\n",
    "\n",
    "stores_city_handler = OneHotProcess(\"stores_city\")\n",
    "stores_city_handler.run_label_handler(df_train_New,'stores_city')\n",
    "one_hot_res = stores_city_handler.one_hot_res\n",
    "df_train_New[\"stores_city\"] = one_hot_res\n",
    "\n",
    "\n",
    "store_state_handler = OneHotProcess(\"store_state\")\n",
    "store_state_handler.run_label_handler(df_train_New,'store_state')\n",
    "one_hot_res = store_state_handler.one_hot_res\n",
    "df_train_New[\"store_state\"] = one_hot_res\n",
    "\n",
    "store_type_handler = OneHotProcess(\"store_type\")\n",
    "store_type_handler.run_label_handler(df_train_New,'store_type')\n",
    "one_hot_res = store_type_handler.one_hot_res\n",
    "df_train_New[\"store_type\"] = one_hot_res\n",
    "df_train_New.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "                        \n",
    "### 在确定的信息中只有transactions数据是无法使用的，因为在testset中，transactions数据是空的。   \n",
    " 16  transactions                       292512 non-null  float64           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3054348 entries, 0 to 3054347\n",
      "Data columns (total 14 columns):\n",
      " #   Column                     Dtype  \n",
      "---  ------                     -----  \n",
      " 0   store_nbr                  int64  \n",
      " 1   family                     int32  \n",
      " 2   sales                      float64\n",
      " 3   onpromotion                int64  \n",
      " 4   Daily_holiday_type         int32  \n",
      " 5   Daily_holiday_locale       int32  \n",
      " 6   Daily_holiday_locale_name  int32  \n",
      " 7   Daily_holiday_description  int32  \n",
      " 8   Daily_holiday_transferred  int64  \n",
      " 9   dcoilwtico                 float64\n",
      " 10  stores_city                int32  \n",
      " 11  store_state                int32  \n",
      " 12  store_type                 int32  \n",
      " 13  store_cluster              int64  \n",
      "dtypes: float64(2), int32(8), int64(4)\n",
      "memory usage: 233.0 MB\n"
     ]
    }
   ],
   "source": [
    "# df_train_New.drop(['family','Daily_holiday_type','Daily_holiday_locale','Daily_holiday_locale_name','Daily_holiday_description',\n",
    "#                    'Daily_holiday_transferred','stores_city','store_state','store_type','transactions'\n",
    "#                    ],axis=1,inplace=True)\n",
    "# 拿到训练集合数据\n",
    "df_train_New.drop(['id','date','transactions'],axis=1,inplace=True)\n",
    "df_train_New.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store_nbr</th>\n",
       "      <th>family</th>\n",
       "      <th>sales</th>\n",
       "      <th>onpromotion</th>\n",
       "      <th>Daily_holiday_type</th>\n",
       "      <th>Daily_holiday_locale</th>\n",
       "      <th>Daily_holiday_locale_name</th>\n",
       "      <th>Daily_holiday_description</th>\n",
       "      <th>Daily_holiday_transferred</th>\n",
       "      <th>dcoilwtico</th>\n",
       "      <th>stores_city</th>\n",
       "      <th>store_state</th>\n",
       "      <th>store_type</th>\n",
       "      <th>store_cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>93.14</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>93.14</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>93.14</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>93.14</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>93.14</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054343</th>\n",
       "      <td>9</td>\n",
       "      <td>28</td>\n",
       "      <td>438.133</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>47.57</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054344</th>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "      <td>154.553</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>47.57</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054345</th>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>2419.729</td>\n",
       "      <td>148</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>47.57</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054346</th>\n",
       "      <td>9</td>\n",
       "      <td>31</td>\n",
       "      <td>121.000</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>47.57</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054347</th>\n",
       "      <td>9</td>\n",
       "      <td>32</td>\n",
       "      <td>16.000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>47.57</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3054348 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         store_nbr  family     sales  onpromotion  Daily_holiday_type  \\\n",
       "0                1       0     0.000            0                   3   \n",
       "1                1       1     0.000            0                   3   \n",
       "2                1       2     0.000            0                   3   \n",
       "3                1       3     0.000            0                   3   \n",
       "4                1       4     0.000            0                   3   \n",
       "...            ...     ...       ...          ...                 ...   \n",
       "3054343          9      28   438.133            0                   3   \n",
       "3054344          9      29   154.553            1                   3   \n",
       "3054345          9      30  2419.729          148                   3   \n",
       "3054346          9      31   121.000            8                   3   \n",
       "3054347          9      32    16.000            0                   3   \n",
       "\n",
       "         Daily_holiday_locale  Daily_holiday_locale_name  \\\n",
       "0                           1                          4   \n",
       "1                           1                          4   \n",
       "2                           1                          4   \n",
       "3                           1                          4   \n",
       "4                           1                          4   \n",
       "...                       ...                        ...   \n",
       "3054343                     0                         19   \n",
       "3054344                     0                         19   \n",
       "3054345                     0                         19   \n",
       "3054346                     0                         19   \n",
       "3054347                     0                         19   \n",
       "\n",
       "         Daily_holiday_description  Daily_holiday_transferred  dcoilwtico  \\\n",
       "0                               50                          0       93.14   \n",
       "1                               50                          0       93.14   \n",
       "2                               50                          0       93.14   \n",
       "3                               50                          0       93.14   \n",
       "4                               50                          0       93.14   \n",
       "...                            ...                        ...         ...   \n",
       "3054343                         28                          0       47.57   \n",
       "3054344                         28                          0       47.57   \n",
       "3054345                         28                          0       47.57   \n",
       "3054346                         28                          0       47.57   \n",
       "3054347                         28                          0       47.57   \n",
       "\n",
       "         stores_city  store_state  store_type  store_cluster  \n",
       "0                 18           12           3             13  \n",
       "1                 18           12           3             13  \n",
       "2                 18           12           3             13  \n",
       "3                 18           12           3             13  \n",
       "4                 18           12           3             13  \n",
       "...              ...          ...         ...            ...  \n",
       "3054343           18           12           1              6  \n",
       "3054344           18           12           1              6  \n",
       "3054345           18           12           1              6  \n",
       "3054346           18           12           1              6  \n",
       "3054347           18           12           1              6  \n",
       "\n",
       "[3054348 rows x 14 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train_New"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取货品类型onehot处理结果，并获取到其列表用于还原数据\n",
    "df_test_New[\"family\"] = family_handler.test_run(df_test_New,'family')\n",
    "df_test_New[\"Daily_holiday_type\"] = Daily_holiday_type_handler.test_run(df_test_New,'Daily_holiday_type')\n",
    "df_test_New[\"Daily_holiday_locale\"] = Daily_holiday_locale_handler.test_run(df_test_New,'Daily_holiday_locale')\n",
    "df_test_New[\"Daily_holiday_locale_name\"] = Daily_holiday_locale_name_handler.test_run(df_test_New,'Daily_holiday_locale_name')\n",
    "df_test_New[\"Daily_holiday_description\"] = Daily_holiday_description_handler.test_run(df_test_New,'Daily_holiday_description')\n",
    "df_test_New[\"Daily_holiday_transferred\"] = Daily_holiday_transferred_handler.test_run(df_test_New,'Daily_holiday_transferred')  \n",
    "df_test_New[\"stores_city\"] = stores_city_handler.test_run(df_test_New,'stores_city')\n",
    "df_test_New[\"store_state\"] = store_state_handler.test_run(df_test_New,'store_state')\n",
    "df_test_New[\"store_type\"] = store_type_handler.test_run(df_test_New,'store_type')\n",
    "df_test_New.drop(['id','date','transactions'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store_nbr</th>\n",
       "      <th>family</th>\n",
       "      <th>onpromotion</th>\n",
       "      <th>Daily_holiday_type</th>\n",
       "      <th>Daily_holiday_locale</th>\n",
       "      <th>Daily_holiday_locale_name</th>\n",
       "      <th>Daily_holiday_description</th>\n",
       "      <th>Daily_holiday_transferred</th>\n",
       "      <th>dcoilwtico</th>\n",
       "      <th>stores_city</th>\n",
       "      <th>store_state</th>\n",
       "      <th>store_type</th>\n",
       "      <th>store_cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>46.80</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>46.80</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>46.80</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>46.80</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>46.80</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28507</th>\n",
       "      <td>9</td>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>47.26</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28508</th>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>47.26</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28509</th>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>47.26</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28510</th>\n",
       "      <td>9</td>\n",
       "      <td>31</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>47.26</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28511</th>\n",
       "      <td>9</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>47.26</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>28512 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       store_nbr  family  onpromotion  Daily_holiday_type  \\\n",
       "0              1       0            0                   6   \n",
       "1              1       1            0                   6   \n",
       "2              1       2            2                   6   \n",
       "3              1       3           20                   6   \n",
       "4              1       4            0                   6   \n",
       "...          ...     ...          ...                 ...   \n",
       "28507          9      28            1                   6   \n",
       "28508          9      29            0                   6   \n",
       "28509          9      30            1                   6   \n",
       "28510          9      31            9                   6   \n",
       "28511          9      32            0                   6   \n",
       "\n",
       "       Daily_holiday_locale  Daily_holiday_locale_name  \\\n",
       "0                         3                         24   \n",
       "1                         3                         24   \n",
       "2                         3                         24   \n",
       "3                         3                         24   \n",
       "4                         3                         24   \n",
       "...                     ...                        ...   \n",
       "28507                     3                         24   \n",
       "28508                     3                         24   \n",
       "28509                     3                         24   \n",
       "28510                     3                         24   \n",
       "28511                     3                         24   \n",
       "\n",
       "       Daily_holiday_description  Daily_holiday_transferred  dcoilwtico  \\\n",
       "0                            101                          0       46.80   \n",
       "1                            101                          0       46.80   \n",
       "2                            101                          0       46.80   \n",
       "3                            101                          0       46.80   \n",
       "4                            101                          0       46.80   \n",
       "...                          ...                        ...         ...   \n",
       "28507                        101                          0       47.26   \n",
       "28508                        101                          0       47.26   \n",
       "28509                        101                          0       47.26   \n",
       "28510                        101                          0       47.26   \n",
       "28511                        101                          0       47.26   \n",
       "\n",
       "       stores_city  store_state  store_type  store_cluster  \n",
       "0               18           12           3             13  \n",
       "1               18           12           3             13  \n",
       "2               18           12           3             13  \n",
       "3               18           12           3             13  \n",
       "4               18           12           3             13  \n",
       "...            ...          ...         ...            ...  \n",
       "28507           18           12           1              6  \n",
       "28508           18           12           1              6  \n",
       "28509           18           12           1              6  \n",
       "28510           18           12           1              6  \n",
       "28511           18           12           1              6  \n",
       "\n",
       "[28512 rows x 13 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test_New"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>dcoilwtico</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>46.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>46.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>46.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>46.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28507</th>\n",
       "      <td>47.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28508</th>\n",
       "      <td>47.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28509</th>\n",
       "      <td>47.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28510</th>\n",
       "      <td>47.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28511</th>\n",
       "      <td>47.26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>28512 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       dcoilwtico\n",
       "0           46.80\n",
       "1           46.80\n",
       "2           46.80\n",
       "3           46.80\n",
       "4           46.80\n",
       "...           ...\n",
       "28507       47.26\n",
       "28508       47.26\n",
       "28509       47.26\n",
       "28510       47.26\n",
       "28511       47.26\n",
       "\n",
       "[28512 rows x 1 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test_New[[\"dcoilwtico\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       dcoilwtico\n",
      "0           46.80\n",
      "1           46.80\n",
      "2           46.80\n",
      "3           46.80\n",
      "4           46.80\n",
      "...           ...\n",
      "28507       47.26\n",
      "28508       47.26\n",
      "28509       47.26\n",
      "28510       47.26\n",
      "28511       47.26\n",
      "\n",
      "[28512 rows x 1 columns]\n",
      "[[46.8 ]\n",
      " [46.8 ]\n",
      " [46.8 ]\n",
      " ...\n",
      " [47.26]\n",
      " [47.26]\n",
      " [47.26]]\n"
     ]
    }
   ],
   "source": [
    "# 创建两个缩放工具\n",
    "sales_sc = MinMaxScaler(feature_range = (0, 1))\n",
    "dcoilwtico_sc = MinMaxScaler(feature_range = (0, 1))\n",
    "\n",
    "train_sales_scaled = sales_sc.fit_transform(df_train_New[[\"sales\"]])\n",
    "df_train_New[\"sales\"] = train_sales_scaled\n",
    "train_dcoiwtico_scaled = dcoilwtico_sc.fit_transform(df_train_New[[\"dcoilwtico\"]])\n",
    "df_train_New[\"dcoilwtico\"] = train_dcoiwtico_scaled\n",
    "\n",
    "# 油价作用于测试集\n",
    "print(df_test_New[[\"dcoilwtico\"]])\n",
    "test_dcoiwtico_scaled = dcoilwtico_sc.fit_transform(df_test_New[[\"dcoilwtico\"]])\n",
    "df_test_New[\"dcoilwtico\"] = test_dcoiwtico_scaled\n",
    "print(dcoilwtico_sc.inverse_transform(df_test_New[[\"dcoilwtico\"]]))\n",
    "\n",
    "# df_train_New"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store_nbr</th>\n",
       "      <th>family</th>\n",
       "      <th>sales</th>\n",
       "      <th>onpromotion</th>\n",
       "      <th>Daily_holiday_type</th>\n",
       "      <th>Daily_holiday_locale</th>\n",
       "      <th>Daily_holiday_locale_name</th>\n",
       "      <th>Daily_holiday_description</th>\n",
       "      <th>Daily_holiday_transferred</th>\n",
       "      <th>dcoilwtico</th>\n",
       "      <th>stores_city</th>\n",
       "      <th>store_state</th>\n",
       "      <th>store_type</th>\n",
       "      <th>store_cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>0.792965</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>0.792965</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>0.792965</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>0.792965</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>0.792965</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054343</th>\n",
       "      <td>9</td>\n",
       "      <td>28</td>\n",
       "      <td>0.003513</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0.253228</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054344</th>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "      <td>0.001239</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0.253228</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054345</th>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>0.019402</td>\n",
       "      <td>148</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0.253228</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054346</th>\n",
       "      <td>9</td>\n",
       "      <td>31</td>\n",
       "      <td>0.000970</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0.253228</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3054347</th>\n",
       "      <td>9</td>\n",
       "      <td>32</td>\n",
       "      <td>0.000128</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0.253228</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3054348 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         store_nbr  family     sales  onpromotion  Daily_holiday_type  \\\n",
       "0                1       0  0.000000            0                   3   \n",
       "1                1       1  0.000000            0                   3   \n",
       "2                1       2  0.000000            0                   3   \n",
       "3                1       3  0.000000            0                   3   \n",
       "4                1       4  0.000000            0                   3   \n",
       "...            ...     ...       ...          ...                 ...   \n",
       "3054343          9      28  0.003513            0                   3   \n",
       "3054344          9      29  0.001239            1                   3   \n",
       "3054345          9      30  0.019402          148                   3   \n",
       "3054346          9      31  0.000970            8                   3   \n",
       "3054347          9      32  0.000128            0                   3   \n",
       "\n",
       "         Daily_holiday_locale  Daily_holiday_locale_name  \\\n",
       "0                           1                          4   \n",
       "1                           1                          4   \n",
       "2                           1                          4   \n",
       "3                           1                          4   \n",
       "4                           1                          4   \n",
       "...                       ...                        ...   \n",
       "3054343                     0                         19   \n",
       "3054344                     0                         19   \n",
       "3054345                     0                         19   \n",
       "3054346                     0                         19   \n",
       "3054347                     0                         19   \n",
       "\n",
       "         Daily_holiday_description  Daily_holiday_transferred  dcoilwtico  \\\n",
       "0                               50                          0    0.792965   \n",
       "1                               50                          0    0.792965   \n",
       "2                               50                          0    0.792965   \n",
       "3                               50                          0    0.792965   \n",
       "4                               50                          0    0.792965   \n",
       "...                            ...                        ...         ...   \n",
       "3054343                         28                          0    0.253228   \n",
       "3054344                         28                          0    0.253228   \n",
       "3054345                         28                          0    0.253228   \n",
       "3054346                         28                          0    0.253228   \n",
       "3054347                         28                          0    0.253228   \n",
       "\n",
       "         stores_city  store_state  store_type  store_cluster  \n",
       "0                 18           12           3             13  \n",
       "1                 18           12           3             13  \n",
       "2                 18           12           3             13  \n",
       "3                 18           12           3             13  \n",
       "4                 18           12           3             13  \n",
       "...              ...          ...         ...            ...  \n",
       "3054343           18           12           1              6  \n",
       "3054344           18           12           1              6  \n",
       "3054345           18           12           1              6  \n",
       "3054346           18           12           1              6  \n",
       "3054347           18           12           1              6  \n",
       "\n",
       "[3054348 rows x 14 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train_New"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store_nbr</th>\n",
       "      <th>family</th>\n",
       "      <th>onpromotion</th>\n",
       "      <th>Daily_holiday_type</th>\n",
       "      <th>Daily_holiday_locale</th>\n",
       "      <th>Daily_holiday_locale_name</th>\n",
       "      <th>Daily_holiday_description</th>\n",
       "      <th>Daily_holiday_transferred</th>\n",
       "      <th>dcoilwtico</th>\n",
       "      <th>stores_city</th>\n",
       "      <th>store_state</th>\n",
       "      <th>store_type</th>\n",
       "      <th>store_cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.319392</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.319392</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.319392</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.319392</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.319392</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28507</th>\n",
       "      <td>9</td>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.494297</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28508</th>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.494297</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28509</th>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.494297</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28510</th>\n",
       "      <td>9</td>\n",
       "      <td>31</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.494297</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28511</th>\n",
       "      <td>9</td>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>101</td>\n",
       "      <td>0</td>\n",
       "      <td>0.494297</td>\n",
       "      <td>18</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>28512 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       store_nbr  family  onpromotion  Daily_holiday_type  \\\n",
       "0              1       0            0                   6   \n",
       "1              1       1            0                   6   \n",
       "2              1       2            2                   6   \n",
       "3              1       3           20                   6   \n",
       "4              1       4            0                   6   \n",
       "...          ...     ...          ...                 ...   \n",
       "28507          9      28            1                   6   \n",
       "28508          9      29            0                   6   \n",
       "28509          9      30            1                   6   \n",
       "28510          9      31            9                   6   \n",
       "28511          9      32            0                   6   \n",
       "\n",
       "       Daily_holiday_locale  Daily_holiday_locale_name  \\\n",
       "0                         3                         24   \n",
       "1                         3                         24   \n",
       "2                         3                         24   \n",
       "3                         3                         24   \n",
       "4                         3                         24   \n",
       "...                     ...                        ...   \n",
       "28507                     3                         24   \n",
       "28508                     3                         24   \n",
       "28509                     3                         24   \n",
       "28510                     3                         24   \n",
       "28511                     3                         24   \n",
       "\n",
       "       Daily_holiday_description  Daily_holiday_transferred  dcoilwtico  \\\n",
       "0                            101                          0    0.319392   \n",
       "1                            101                          0    0.319392   \n",
       "2                            101                          0    0.319392   \n",
       "3                            101                          0    0.319392   \n",
       "4                            101                          0    0.319392   \n",
       "...                          ...                        ...         ...   \n",
       "28507                        101                          0    0.494297   \n",
       "28508                        101                          0    0.494297   \n",
       "28509                        101                          0    0.494297   \n",
       "28510                        101                          0    0.494297   \n",
       "28511                        101                          0    0.494297   \n",
       "\n",
       "       stores_city  store_state  store_type  store_cluster  \n",
       "0               18           12           3             13  \n",
       "1               18           12           3             13  \n",
       "2               18           12           3             13  \n",
       "3               18           12           3             13  \n",
       "4               18           12           3             13  \n",
       "...            ...          ...         ...            ...  \n",
       "28507           18           12           1              6  \n",
       "28508           18           12           1              6  \n",
       "28509           18           12           1              6  \n",
       "28510           18           12           1              6  \n",
       "28511           18           12           1              6  \n",
       "\n",
       "[28512 rows x 13 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test_New"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_229460\\3135102971.py:11: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  train_df.drop([\"sales\"],axis=1,inplace=True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_229460\\3135102971.py:12: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  val_df.drop([\"sales\"],axis=1,inplace=True)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         store_nbr  family  onpromotion  Daily_holiday_type  \\\n",
      "2443478         19      26            0                   6   \n",
      "2443479         19      27            0                   6   \n",
      "2443480         19      28            0                   6   \n",
      "2443481         19      29            1                   6   \n",
      "2443482         19      30            1                   6   \n",
      "...            ...     ...          ...                 ...   \n",
      "3054343          9      28            0                   3   \n",
      "3054344          9      29            1                   3   \n",
      "3054345          9      30          148                   3   \n",
      "3054346          9      31            8                   3   \n",
      "3054347          9      32            0                   3   \n",
      "\n",
      "         Daily_holiday_locale  Daily_holiday_locale_name  \\\n",
      "2443478                     3                         24   \n",
      "2443479                     3                         24   \n",
      "2443480                     3                         24   \n",
      "2443481                     3                         24   \n",
      "2443482                     3                         24   \n",
      "...                       ...                        ...   \n",
      "3054343                     0                         19   \n",
      "3054344                     0                         19   \n",
      "3054345                     0                         19   \n",
      "3054346                     0                         19   \n",
      "3054347                     0                         19   \n",
      "\n",
      "         Daily_holiday_description  Daily_holiday_transferred  dcoilwtico  \\\n",
      "2443478                        101                          0    0.237949   \n",
      "2443479                        101                          0    0.237949   \n",
      "2443480                        101                          0    0.237949   \n",
      "2443481                        101                          0    0.237949   \n",
      "2443482                        101                          0    0.237949   \n",
      "...                            ...                        ...         ...   \n",
      "3054343                         28                          0    0.253228   \n",
      "3054344                         28                          0    0.253228   \n",
      "3054345                         28                          0    0.253228   \n",
      "3054346                         28                          0    0.253228   \n",
      "3054347                         28                          0    0.253228   \n",
      "\n",
      "         stores_city  store_state  store_type  store_cluster  \n",
      "2443478            7            1           2             15  \n",
      "2443479            7            1           2             15  \n",
      "2443480            7            1           2             15  \n",
      "2443481            7            1           2             15  \n",
      "2443482            7            1           2             15  \n",
      "...              ...          ...         ...            ...  \n",
      "3054343           18           12           1              6  \n",
      "3054344           18           12           1              6  \n",
      "3054345           18           12           1              6  \n",
      "3054346           18           12           1              6  \n",
      "3054347           18           12           1              6  \n",
      "\n",
      "[610870 rows x 13 columns]\n",
      "[[ 19.  26.   0. ...   1.   2.  15.]\n",
      " [ 19.  27.   0. ...   1.   2.  15.]\n",
      " [ 19.  28.   0. ...   1.   2.  15.]\n",
      " ...\n",
      " [  9.  30. 148. ...  12.   1.   6.]\n",
      " [  9.  31.   8. ...  12.   1.   6.]\n",
      " [  9.  32.   0. ...  12.   1.   6.]]\n"
     ]
    }
   ],
   "source": [
    "# 切分数据集，其中训练数据占80%\n",
    "num_shape = round(len(df_train_New) * 0.8)\n",
    "val_num_shape = len(df_train_New) - num_shape\n",
    "\n",
    "train_df = df_train_New[:num_shape]\n",
    "val_df = df_train_New[num_shape:]\n",
    "\n",
    "# 获取标签\n",
    "train_df_label =  train_df[\"sales\"]\n",
    "val_df_label =  val_df[\"sales\"]\n",
    "train_df.drop([\"sales\"],axis=1,inplace=True)\n",
    "val_df.drop([\"sales\"],axis=1,inplace=True)\n",
    "\n",
    "# 从数值上进行切分获取数据列表\n",
    "train = train_df.iloc[:, 0:16].values\n",
    "train_label =  train_df_label.iloc[:].values\n",
    "print(val_df)\n",
    "val = val_df.iloc[:, 0:16].values\n",
    "val_label = val_df_label.iloc[:].values\n",
    "print(val)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   store_nbr  family  onpromotion  Daily_holiday_type  Daily_holiday_locale  \\\n",
      "0          1       0            0                   3                     1   \n",
      "1          1       1            0                   3                     1   \n",
      "2          1       2            0                   3                     1   \n",
      "3          1       3            0                   3                     1   \n",
      "\n",
      "   Daily_holiday_locale_name  Daily_holiday_description  \\\n",
      "0                          4                         50   \n",
      "1                          4                         50   \n",
      "2                          4                         50   \n",
      "3                          4                         50   \n",
      "\n",
      "   Daily_holiday_transferred  dcoilwtico  stores_city  store_state  \\\n",
      "0                          0    0.792965           18           12   \n",
      "1                          0    0.792965           18           12   \n",
      "2                          0    0.792965           18           12   \n",
      "3                          0    0.792965           18           12   \n",
      "\n",
      "   store_type  store_cluster  \n",
      "0           3             13  \n",
      "1           3             13  \n",
      "2           3             13  \n",
      "3           3             13  \n",
      "(2443478, 13)\n"
     ]
    }
   ],
   "source": [
    "print(train_df.head(4))\n",
    "print(train_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.          0.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          1.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          2.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          3.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          4.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          5.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          6.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          7.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          8.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]\n",
      " [ 1.          9.          0.          3.          1.          4.\n",
      "  50.          0.          0.79296459 18.         12.          3.\n",
      "  13.        ]]\n",
      "(2443478, 13)\n"
     ]
    }
   ],
   "source": [
    "print(train[0:10])\n",
    "print(train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2443416, 62, 13]) torch.Size([2443416])\n",
      "torch.Size([610808, 62, 13]) torch.Size([610808])\n"
     ]
    }
   ],
   "source": [
    "window = 62\n",
    "feature_dim = train.shape[1]\n",
    "\n",
    "X_train = []\n",
    "y_train = []\n",
    "for i in range(window, num_shape):\n",
    "    X_train.append(train[i-window:i])\n",
    "    y_train.append(train_label[i])\n",
    "    \n",
    "X_train = torch.Tensor(np.stack(X_train)).reshape(-1, window , feature_dim)\n",
    "y_train = torch.Tensor(np.stack(y_train))\n",
    "print(X_train.shape,y_train.shape)\n",
    "\n",
    "X_val = []\n",
    "y_val = []\n",
    "for i in range(window, val_num_shape):\n",
    "    X_val.append(val[i-window:i])\n",
    "    y_val.append(val_label[i])\n",
    "    \n",
    "X_val = torch.Tensor(np.stack(X_val)).reshape(-1, window , feature_dim)\n",
    "y_val = torch.Tensor(np.stack(y_val))\n",
    "print(X_val.shape,y_val.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "搭建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TimeSeqDataEncoder(d2l.Encoder):\n",
    "    \"\"\"用于序列到序列学习的循环神经网络编码器\"\"\"\n",
    "\n",
    "    def __init__(self, embed_size, num_hiddens, num_layers,\n",
    "                 dropout=0, **kwargs):\n",
    "        super(TimeSeqDataEncoder, self).__init__(**kwargs)\n",
    "        # # 嵌入层\n",
    "        # self.embedding = nn.Embedding(vocab_size, embed_size)\n",
    "        self.rnn = nn.LSTM(embed_size, num_hiddens, num_layers,batch_first=True, dropout=dropout if num_layers > 1 else 0)\n",
    "        self.dropout = nn.Dropout(dropout)  \n",
    "        self.fc = nn.Linear(num_hiddens, 1)\n",
    "\n",
    "    def forward(self, X, *args):\n",
    "        output, _ = self.rnn(X)\n",
    "        # 取最后一个时间步的输出  \n",
    "        output = self.dropout(output[:, -1, :])  \n",
    "        # print(output.shape)\n",
    "        output = self.fc(output).squeeze(1)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "###################\n",
      "torch.Size([2443416, 62, 13])\n",
      "2443416\n",
      "torch.Size([32])\n"
     ]
    }
   ],
   "source": [
    "print(\"###################\")\n",
    "print(X_train.shape)\n",
    "print(len(X_train))\n",
    "ts_model  = TimeSeqDataEncoder(13,64,3,0.2)\n",
    "output = ts_model(torch.randn(32, 62, 13))\n",
    "print(output.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(tensor([[ 1.0000,  0.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  1.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  2.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  3.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  4.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  5.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  6.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  7.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  8.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  9.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 10.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 11.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 12.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 13.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 14.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 15.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 16.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 17.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 18.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 19.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 20.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 21.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 22.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 23.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 24.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 25.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 26.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 27.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 28.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 29.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 30.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 31.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 32.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [10.0000,  0.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  1.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  2.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  3.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  4.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  5.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  6.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  7.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  8.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  9.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 10.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 11.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 12.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 13.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 14.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 15.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 16.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 17.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 18.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 19.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 20.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 21.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 22.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 23.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 24.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 25.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 26.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 27.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 28.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000]]), tensor(0.)), (tensor([[ 1.0000,  1.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  2.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  3.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  4.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  5.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  6.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  7.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  8.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000,  9.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 10.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 11.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 12.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 13.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 14.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 15.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 16.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 17.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 18.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 19.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 20.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 21.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 22.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 23.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 24.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 25.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 26.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 27.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 28.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 29.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 30.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 31.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [ 1.0000, 32.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  3.0000, 13.0000],\n",
      "        [10.0000,  0.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  1.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  2.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  3.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  4.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  5.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  6.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  7.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  8.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000,  9.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 10.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 11.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 12.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 13.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 14.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 15.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 16.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 17.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 18.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 19.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 20.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 21.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 22.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 23.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 24.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 25.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 26.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 27.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 28.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000],\n",
      "        [10.0000, 29.0000,  0.0000,  3.0000,  1.0000,  4.0000, 50.0000,  0.0000,\n",
      "          0.7930, 18.0000, 12.0000,  2.0000, 15.0000]]), tensor(0.))]\n",
      "0 torch.Size([128, 62, 13]) torch.Size([128])\n"
     ]
    }
   ],
   "source": [
    "#in pcdet/datasets/__init__.py\n",
    "batch_size = 128\n",
    "from  torch.utils.data import DataLoader\n",
    "train_pairs = [(x, y) for x, y in zip(X_train, y_train)]  \n",
    "print(train_pairs[0:2])\n",
    "train_data = DataLoader(\n",
    "        train_pairs,\n",
    "        batch_size=batch_size,\n",
    "        pin_memory=True,\n",
    "        num_workers=0,\n",
    "        shuffle=True,\n",
    "        collate_fn=None, #将一个list的sample组成一个mini-batch的函数\n",
    "        drop_last=False\n",
    "    )\n",
    "for i, (input,label) in enumerate(train_data):\n",
    "    print(i,input.shape,label.shape)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设你已经有了定义的模型 model  \n",
    "# 定义优化器  \n",
    "\n",
    "\n",
    "optimizer = torch.optim.Adam(ts_model.parameters(), lr=0.001)  # lr是学习率  \n",
    "# 定义损失函数  \n",
    "criterion = nn.MSELoss()  # 均方误差损失函数  \n",
    "num_epochs = 100\n",
    "device = \"cuda\"\n",
    "\n",
    "ts_model = ts_model.to(device)\n",
    "# 接下来，你可以在训练循环中使用这些组件来训练模型  \n",
    "for epoch in range(num_epochs): \n",
    "    for i, (inputs,targets) in enumerate(train_data):\n",
    "        # print(i,inputs.shape,targets.shape)\n",
    "        # print(\"targets\",targets)\n",
    "        # 将输入和目标数据转移到GPU（如果可用）  \n",
    "        inputs, targets = inputs.to(device), targets.to(device)  \n",
    "        # 前向传播  \n",
    "        outputs = ts_model(inputs)  \n",
    "        # 计算损失  \n",
    "        loss = criterion(outputs, targets)  \n",
    "        # 清除之前的梯度（如果存在）  \n",
    "        optimizer.zero_grad()  \n",
    "        # 反向传播  \n",
    "        loss.backward()  \n",
    "        # 更新权重  \n",
    "        optimizer.step()  \n",
    "        # 打印统计信息（可选）  \n",
    "        # if (i+1) % 100 == 0:  \n",
    "        #     print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{len(dataloader)}], Loss: {loss.item():.4f}')  \n",
    "        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')\n",
    "  \n",
    "# 注意：device 是你用来指定在哪个设备上运行模型（CPU或GPU）的变量  \n",
    "# 例如: device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
